package com.ac.controller;

import javax.servlet.http.HttpServletRequest;

import org.bson.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.exception.RRException;
import com.ac.model.AuthUser;
import com.ac.model.Page;
import com.ac.model.query.AuthUserQ;
import com.ac.response.HttpStatus;
import com.ac.response.Res;
import com.ac.service.AuthTokenService;
import com.ac.service.AuthUserService;
import com.ac.util.IPUtils;
import com.ac.util.MapUtils;
import com.ac.util.StrUtils;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("auth/user")
@RequiredArgsConstructor
public class AuthUserController {

	private final AuthUserService UserService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token, @RequestBody Document map) {
		AuthUserQ uq = MapUtils.toBean(map, AuthUserQ.class);
		String parentId = ts.getUserId(token);
		uq.setParentId(parentId);
		Page page = UserService.list(uq);
		Res res = Res.success(null, page.getList(), page.getTotal());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody AuthUser user, @RequestHeader("token") String token) {
		String parentId = ts.getUserId(token);
		if (StrUtils.isEmpty(parentId))
			throw new RRException("非法操作", HttpStatus.BUG);
		if (StrUtils.isEmpty(user.getId())) {
			user.setParentId(parentId);
			UserService.insert(user);
		} else {
			String parentIdu = user.getParentId();
			if (parentIdu.equals(parentId)) {
				UserService.update(user);
			} else {
				throw new RRException("非法操作", HttpStatus.BUG);
			}
		}
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "remove")
	public Res remove(@RequestBody AuthUser user, @RequestHeader("token") String token) {
		UserService.remove(user);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

	@PostMapping(value = "login")
	public Res login(HttpServletRequest request, @RequestBody Document map,
			@RequestHeader("User-Agent") String userAgent, @RequestHeader("token") String token) {
		map.append("userAgent", userAgent);
		map.append("ip", IPUtils.getIpAddr(request));
		Res res = Res.success("登录成功", UserService.login(map));
		return res;
	}

	@Auth
	@PostMapping(value = "resetPW")
	public Res resetPW(HttpServletRequest request, @RequestBody Document map, @RequestHeader("token") String token) {
		String userId = ts.getUserId(token);
		map.append("__userId", userId);
		UserService.resetPW(map);
		Res res = Res.success("修改密码成功");
		return res;
	}

	@Auth
	@PostMapping(value = "info")
	public Res info(@RequestHeader("token") String token) {
		String userId = ts.getUserId(token);
		AuthUser info = UserService.info(userId,false);
		info.setId(null);
		Res res = Res.success(null, info);
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "saveInfo")
	public Res saveInfo(@RequestHeader("token") String token, @RequestBody AuthUser user) {
		String userId = ts.getUserId(token);
		user.setId(userId);
		UserService.saveInfo(user);
		Res res = Res.success(null, "保存成功");
		return ts.getToken(res, token);
	}

}
